<?php
session_start();
// error_reporting(E_ALL);
// ini_set('display_errors', '1');

function dbconectar()
{
   include '../conf/config.php';
   $enlace = mysql_connect("$dbhost","$dbuser","$dbpass") or die("No puedo conectarme a la base de datos");
   mysql_set_charset('latin1',$enlace);

   mysql_select_db("$dbname") or die("No puedo seleccionar la base de datos");
}

function dbdesconectar()
{
   mysql_close();
}

function validateLogin($username, $password)
{
  unset($_SESSION['login']);
  unset($_SESSION['fullname']);
  unset($_SESSION['function']);
  dbconectar();

  $encryptedPassword=md5($password);
  
  $userSearch=mysql_query("select count(*) as quantity from people where login='$username' and passwd='$encryptedPassword'");
  while ($row=mysql_fetch_array($userSearch)) {
    $exists=$row['quantity'];
  }
  dbdesconectar();
  if ( $exists > 0 ) {
    $output=1;
  } else {
    $output=0;
  }
  
  return $output;
}

function phrase($idioma, $frase) 
{
	dbconectar();
	$phraseSearch=mysql_query("select traduccion from frases where frase='$frase' and idioma='$idioma'");
	while ($row=mysql_fetch_array($phraseSearch)) {
		$value0=$row['traduccion'];
	}
	dbdesconectar();
	return $value0;
}

function validUser($login,$function) {
  echo "Valid User";
}

function menuItemAccess($username)
{
	dbconectar();
	$groupSearch=mysql_query("select a.gru_id from rel_grupos_usuarios a, people b where b.login='$username' and a.usu_id=b.peo_id");
	dbdesconectar();
}

function groupsList() {
  include '../conf/config.php';
  $output=array();
  
  dbconectar();
  $groupsSearch=mysql_query("select * from grupos order by grupo_nombre");
  while ($row=mysql_fetch_array($groupsSearch)) {
    $groupId=$row['gru_id'];
    $groupName=$row['grupo_nombre'];
    $groupDescription=$row['grupo_descripcion'];
    $output[]=array("$groupId","$groupName","$groupDescription");
  }
  dbdesconectar();
  
  return($output);
}

function groupDetails($item,$groupId){
  switch ($item)
  {
    case "groupName":
      dbconectar();
      $groupNameSearch=mysql_query("select grupo_nombre from grupos where gru_id='$groupId'");
      while($row=mysql_fetch_array($groupNameSearch))
      {
        $output=$row['grupo_nombre'];
      }
      dbdesconectar();
      break;
      
    case "groupDescription":
      dbconectar();
      $query="select grupo_descripcion from grupos where gru_id='$groupId'";
      $groupDescriptionSearch=mysql_query($query);
      while($row=mysql_fetch_array($groupDescriptionSearch))
      {
        $output=$row['grupo_descripcion'];
        
        if ( $output == "" )
        {
          $output="N/D";
        }
        
      }
      dbdesconectar();
      break;
      
    default:
      $output="No disponible";
  }
  
  return ($output);
}

function groupNameFromGroupId($groupId) {
  dbconectar();
  $nameSearch=mysql_query("select grupo_nombre from grupos where gru_id='$groupId'");
  while ($row=mysql_fetch_array($nameSearch)) {
    $groupName=$row['grupo_nombre'];
  }
  dbdesconectar();
  return $groupName;
}

function groupDescriptionFromGroupId($groupId) {
  dbconectar();
  $descriptionSearch=mysql_query("select grupo_descripcion from grupos where gru_id='$groupId'");
  while ($row=mysql_fetch_array($descriptionSearch)) {
    $groupDescription=$row['grupo_descripcion'];
  }
  dbdesconectar();
  return $groupDescription;
}

function groupMembers($groupId) {
  dbconectar();
  $usersSearch=mysql_query("select a.gru_id as groupId, a.grupo_nombre as groupName, a.grupo_descripcion as groupDescription, b.rel_id as relationId, c.peo_id as userId, c.login as login, c.fullname as fullname from grupos a, rel_grupos_usuarios b, people c where a.gru_id='$groupId' and b.gru_id=a.gru_id and b.usu_id=c.peo_id");
  while ($row=mysql_fetch_array($usersSearch)) {
    $userId=$row['userId'];
    $userLogin=$row['login'];
    $userFullname=$row['fullname'];
    echo "<tr>";
    echo "<td class=\"tdh2\" width=\"200\"><div align=\"center\"><a href=\"./desktop.php?function=grupo_details&groupId=$userId\">" . $userLogin . "</a></div></td>";
    echo "<td class=\"tdh2\" width=\"100%\"><div align=\"left\">" . $userFullname . "</div></td>";
    echo "</tr>";
  }
  dbdesconectar();
}

function companiesList() {
  include '../conf/config.php';
  $output=array();
  dbconectar();
  $companiesSearch=mysql_query("select * from $customersTable order by razon_social");
  while ($row=mysql_fetch_array($companiesSearch)) {
    $companyId=$row['_rowid'];
    $companyName=$row['razon_social'];
    $companyPhone=$row['telefono'];

    $output[]=array("$companyId", "$companyName", "$companyPhone");
  }
  dbdesconectar();
  return ($output);
}

function companyDetails($item,$companyId)
{
  include '../conf/config.php';
  switch ($item)
  {
    case "companyName":
      dbconectar();
      $query=mysql_query("select razon_social as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
   
    case "companyAddressStreet":
      dbconectar();
      $query=mysql_query("select addressstreet as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;

    case "companyAddressNumber":
      dbconectar();
      $query=mysql_query("select addressnumber as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyAddressFloor":
      dbconectar();
      $query=mysql_query("select addressfloor as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyAddressOffice":
      dbconectar();
      $query=mysql_query("select addressoffice as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyAddressPO":
      dbconectar();
      $query=mysql_query("select addresspo as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyAddressTown":
      dbconectar();
      $query=mysql_query("select addresstown as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyAddressState":
      dbconectar();
      $query=mysql_query("select b.state as item from $customersTable a, $statesTable b where a._rowid='$companyId' and a.addressstate=b._rowid");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyAddressCountry":
      dbconectar();
      $query=mysql_query("select b.country as item from $customersTable a, $countriesTable b where a._rowid='$companyId' and a.addresscountry=b._rowid");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressStreet":
      dbconectar();
      $query=mysql_query("select billingaddressstreet as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;

    case "companyBillingAddressNumber":
      dbconectar();
      $query=mysql_query("select billingaddressnumber as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressFloor":
      dbconectar();
      $query=mysql_query("select billingaddressfloor as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressOffice":
      dbconectar();
      $query=mysql_query("select billingaddressoffice as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressPO":
      dbconectar();
      $query=mysql_query("select billingaddresspo as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressTown":
      dbconectar();
      $query=mysql_query("select billingaddresstown as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressState":
      dbconectar();
      $query=mysql_query("select b.state as item from $customersTable a, $statesTable b where a._rowid='$companyId' and a.billingaddressstate=b._rowid");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyBillingAddressCountry":
      dbconectar();
      $query=mysql_query("select b.country as item from $customersTable a, $countriesTable b where a._rowid='$companyId' and a.billingaddresscountry=b._rowid");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyPhone":
      dbconectar();
      $query=mysql_query("select telefono as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyActivation":
      dbconectar();
      $query=mysql_query("select activation as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyNumber":
      dbconectar();
      $query=mysql_query("select number as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyCondition":
      dbconectar();
      $query=mysql_query("select taxcondition as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyTaxCodeType":
      dbconectar();
      $query=mysql_query("select taxcodetype as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
      case "companyTaxCode":
      dbconectar();
      $query=mysql_query("select taxcode as item from $customersTable where _rowid='$companyId'");
      while ($row=mysql_fetch_array($query))
      {
        $output=$row['item'];
      }
      dbdesconectar();
      break;
      
    default:
      $output="Item no disponible";
  }
  
  return ($output);
  
}

function companyNameFromId($companyId)
{
  include '../conf/config.php';
  dbconectar();
  $companyNameSearch=mysql_query("select razon_social from $customersTable where _rowid='$companyId'");
  while ($row=mysql_fetch_array($companyNameSearch)) {
    $output=$row['razon_social'];
  }
  dbdesconectar();
  
  return $output;
}

function contactsList()
{
  include '../conf/config.php';
  $output=array();
  dbconectar();
  $contactsSearch=mysql_query("select * from $contactsTable order by _rowid");
  while ($row=mysql_fetch_array($contactsSearch)) {
    $contactId=$row['peo_id'];
    $contactLastName=$row['lastname'];
    $contactFirstName=$row['firstname'];
    $contactPhone=$row['phone'];
    $contactEMail=$row['email'];

    $output[]=array("$contactId", "$contactLastName", "$contactFirstName","$contactPhone","$contactEMail");
  }
  dbdesconectar();
  return ($output);
}

function countriesList()
{
  include '../conf/config.php';
  $output=array();
  dbconectar();
  $countriesSearch=mysql_query("select * from countries order by _rowid");
  while ($row=mysql_fetch_array($countriesSearch))
  {
    $countryId=$row['_rowid'];
    $countryName=$row['country'];
    
    $output[]=array("$countryId","$countryName");
  }
  dbdesconectar();
  
  return ($output);
}

function insertCompany($companyName,$companyPhone,$companyAddressStreet,$companyAddressNumber,$companyAddressFloor,$companyAddressOffice,$companyAddressPO,$companyAddressTown,$companyAddressState,$companyAddressCountry,$companyNumber,$companyCondition)
{
  include '../conf/config.php';
  $activationDate = date('Y-m-d');
  dbconectar();
  mysql_query("insert into $customersTable values ('','$companyName','$companyPhone','$companyAddressStreet','$companyAddressNumber','$companyAddressFloor','$companyAddressOffice','$companyAddressPO','$companyAddressTown','$companyAddressState','$companyAddressCountry','$companyNumber','$companyCondition','$activationDate')");
  dbdesconectar();
}

function invoicesList() 
{
  include '../conf/config.php';
  dbconectar();
  $invoicesSearch=mysql_query("select * from $invoicesTable order by _rowid");
  while ($row=mysql_fetch_array($invoicesSearch)) {
    $invoiceId=$row['_rowid'];
    $invoiceDate=$row['date'];
    $invoiceCustomer=$row['customer'];
    $invoiceAmount=$row['amount'];
    echo "<tr>";
    echo "<td class=\"tdh2\" width=\"\"><div align=\"center\"><a href=\"./desktop.php?function=company_detail&companyId=$invoiceId\">" . $invoiceId . "</a></div></td>";
    echo "<td class=\"tdh2\" width=\"100%\"><div align=\"left\">" . $invoiceDate . "</div></td>";
    echo "<td class=\"tdh2\" width=\"\"><div align=\"left\">" . $invoiceCustomer . "</div></td>";
    echo "<td class=\"tdh2\" width=\"\"><div align=\"left\">" . $invoiceAmount . "</div></td>";
    echo "</tr>";
  }
  dbdesconectar();
}

function rolesList() {
  include '../conf/config.php';
  $output=array();

  dbconectar();
  $rolesSearch=mysql_query("select * from roles order by rol_id");
  while ($row=mysql_fetch_array($rolesSearch)) {
    $roleId=$row['rol_id'];
    $roleName=$row['rol_name'];
    $roleDescription=$row['rol_description'];
    
    $output[]=array("$roleId","$roleName","$roleDescription");
  }
  dbdesconectar();
  
  return($output);
}

function roleIDtoroleName($roleId) {
	dbconectar();
	$nameSearch=mysql_query("select rol_name from roles where rol_id='$roleId'");
	while ($row=mysql_fetch_array($nameSearch)) {
		$roleName=$row['rol_name'];
	}
	dbdesconectar();
	return $roleName;
}

function roleDescriptionFromRoleId($roleId) {
	dbconectar();
	$descriptionSearch=mysql_query("select rol_description from roles where rol_id='$roleId'");
	while ($row=mysql_fetch_array($descriptionSearch)) {
		$roleDescription=$row['rol_description'];
	}
	dbdesconectar();
	return $roleDescription;
}

function statesList()
{
  include '../conf/config.php';
  $output=array();
  dbconectar();
  $statesSearch=mysql_query("select * from states order by _rowid");
  while ($row=mysql_fetch_array($statesSearch))
  {
    $stateId=$row['_rowid'];
    $stateName=$row['state'];
    
    $output[]=array("$stateId","$stateName");
  }
  dbdesconectar();
  
  return ($output);
}

function subrolesListFromRoleId($roleId) {
/* tener en cuenta recursividad y una leyenda si no esta compuesto por otros roles */
  dbconectar();
  $subrolesCount=mysql_query("select count(*) as quantity from roles where rol_parent='$roleId'");
  while ($row=mysql_fetch_array($subrolesCount)) {
    $quantity=$row['quantity'];
  }
  
  if ( $quantity == '0' )
  {
    echo "<tr>";
      echo "<td class=\"tdh2\" width=\"100%\" align=\"center\">Este rol no posee subroles</td>";
    echo "</tr>";
  } else {
    $subrolesSearch=mysql_query("");
    while ($row=mysql_fetch_array($subrolesSearch)) {
      echo "<tr>";
      echo "<td class=\"tdh2\" width=\"200\"><div align=\"center\"><a href=\"./desktop.php?function=role_detail&roleID=$roleID\">" . $roleName . "</a></div></td>";
      echo "<td class=\"tdh2\" width=\"100%\"><div align=\"left\">" . $roleDescription . "</div></td>";
      echo "</tr>";
    }
  }
  dbdesconectar();
}

function groupUsersList($grupo) {
	dbconectar();
	$usersSearch=mysql_query("select a.login as username, a.fullname as user from people a, rel_grupos_usuarios b, grupos c where grupo_nombre='$grupo' and c.gru_id=b.gru_id and a.peo_id=b.usu_id");
	while ($row=mysql_fetch_array($usersSearch)) {
		$value0=$row['username'];
		$value1=$row['user'];
		echo "<tr>";
    	echo "<td width=\"200\" class=\"rounded-corners\" align=\"center\">" . $value0 . "</a></td>";
    	echo "<td width=\"400\" class=\"rounded-corners\" align=\"center\">" . $value1 . "</td>";
    	echo "</tr>";
	}
	dbdesconectar();
}

function usersList($groupId) {
  include '../conf/config.php';
  $output=array();
  dbconectar();
  
  if (isset($groupId))
  {
    $modifier1="and b.gru_id='$groupId'";
  } else {
    $modifier1="";
  }
  
  $usersSearch=mysql_query("select a.* from $contactsTable a, $contactsGroupRelTable b where a.peo_id=b.usu_id $modifier1 order by a.peo_id");
  while ($row=mysql_fetch_array($usersSearch)) {
    $userId=$row['peo_id'];
    $userLogin=$row['login'];
    $lastName=$row['lastname'];
    $firstName=$row['firstname'];
    $userPhone=$row['phone'];
    $userEMail=$row['email'];
    
    $output[]=array("$userId","$userLogin","$lastName","$firstName","$userPhone","$userEMail");
  }
  dbdesconectar();
  
  return($output);
}

function userDetails($userId)
{
  $output=array();
  dbconectar();
  $detailsSearch=mysql_query("select * from people where peo_id='$userId'");
  while($row=mysql_fetch_array($detailsSearch))
  {
    $userLogin=$row['login'];
    $firstName=$row['firstname'];
    if ( $firstName == '' )
    {
      $firstName='N/D';
    }
    $lastName=$row['lastname'];
    if ( $lastName == '' )
    {
      $lastName='N/D';
    }
    $userPhone=$row['phone'];
    if ( $userPhone == '' )
    {
      $userPhone='N/D';
    }
    $userEMail=$row['email'];
    if ( $userEMail == '' )
    {
      $userEMail='N/D';
    }
    
    $output[]=array("$userLogin", "$firstName", "$lastName", "$userPhone", "$userEMail");
  }
  
  dbdesconectar();
  
  return($output);
}

function financeCategoriesList() {
	dbconectar();
	$categoriesSearch=mysql_query("select * from financeCategories");
	while ($row=mysql_fetch_array($categoriesSearch)) {
		$catID=$row['catID'];
		$catParentID=$row['catParentID'];
		$catName=$row['catName'];
		$catDescription=$row['catDescription'];
		echo "<tr>";
		echo "<td>" . $catID . "</td>";
		echo "<td>" . $catParentID . "</td>";
		echo "<td>" . $catName . "</td>";
		echo "</tr>";
	}
	dbdesconectar();
}

function slipPriorities()
{
  $output=array();
  dbconectar();
  $query="select * from slippriorities order by priority";
  $prioritiesSearch=mysql_query($query);
  while($row=mysql_fetch_array($prioritiesSearch))
  {
    $priority=$row['priority'];
    $description=$row['description'];
    $properties=$row['properties'];
    $output[]=array("$priority", "$description", "$properties");
  }
  dbdesconectar();
  return ($output);
}

function slipsList() {
  $output=array();
  dbconectar();
  $slipsSearch=mysql_query("select * from slips order by sid");
  while ($row=mysql_fetch_array($slipsSearch)) {
    $slipId=$row['sid'];
    $summary=$row['problem'];
    $openDateTemp=$row['o_date'];
    $openDate=date("d/m/Y",$openDateTemp);
    $openTech=$row['o_tech'];
    $currentTech=$row['currtech'];
    $closeDateTemp=$row['c_date'];
    $slipStatus=$row['status'];
    
    if ($closeDateTemp == '0')
      $closeDate="Abierto";
    else
      $closeDate=date("d/m/Y",$closeDateTemp);

    $priority=$row['priority'];

    $output[]=array("$slipId", "$summary", "$openDate", "$openTech", "$currentTech", "$closeDate", "$priority", "$slipStatus");
  }
  dbdesconectar();
  return ($output);
}

function slipDetails($slipId) {
	dbconectar();
	$detailsSearch=mysql_query("select * from slips where sid='$slipId'");
	while ($row=mysql_fetch_array($detailsSearch)) {

		$roleName=$row['rol_name'];
	}
	dbdesconectar();
}

function followupList($slipId) {
  $output=array();
  dbconectar();
  $followupsSearch=mysql_query("select * from followups where f_sid='$slipId'");
  while ($row=mysql_fetch_array($followupsSearch)) {
    $followupNr=$row['f_seq'];
    $followupDateTemp=$row['f_date'];
    $followupDate=date("d/m/Y H:i",$followupDateTemp);
    $followupAuthor=$row['f_tech'];
    $followupDetails=$row['f_text'];
    $followupEffort=$row['f_time'];
    $followupEffortType=['tipo_tiempo'];
    
    $output[]=array("$followupNr", "$followupAuthor", "$followupDate", "$followupDetails", "$followupEffort", "$followupEffortType");
  }
  dbdesconectar();
  return ($output);
}

/* TODO */
function assetsList() {
	dbconectar();
	$assetsSearch=mysql_query("select * from slips order by sid");
	while ($row=mysql_fetch_array($assetsSearch)) {
		$slipId=$row['sid'];
		$summary=$row['problem'];
                $openDateTemp=$row['o_date'];
                $openDate=date("d/m/Y",$openDateTemp);
                $openTech=$row['o_tech'];
                $currentTech=$row['currtech'];
                $closeDateTemp=$row['c_date'];

                if ($closeDateTemp == '0')
                  $closeDate="Abierto";
                else
                  $closeDate=date("d/m/Y",$closeDateTemp);

                $priority=$row['priority'];;
		echo "<tr>";
    	echo "<td class=\"tdh2\" width=\"40\"><div align=\"center\"><a href=\"./desktop.php?function=slip_details&slipId=$slipId\">" . $slipId . "</a></div></td>";
    	echo "<td class=\"tdh2\" width=\"500\"><div align=\"left\">" . $summary . "</div></td>";
    	echo "<td class=\"tdh2\" width=\"80\"><div align=\"center\">" . $openDate . "</div></td>";
    	echo "<td class=\"tdh2\" width=\"80\"><div align=\"center\">" . $openTech . "</div></td>";
    	echo "<td class=\"tdh2\" width=\"80\"><div align=\"center\">" . $currentTech . "</div></td>";
    	echo "<td class=\"tdh2\" width=\"80\"><div align=\"center\">" . $closeDate . "</div></td>";
    	echo "<td class=\"tdh2\" width=\"20\"><div align=\"center\">" . $priority . "</div></td>";
    	echo "</tr>";
	}
	dbdesconectar();
}

function slipSummaryFromSlipId($slipId) {
  dbconectar();
  $slipSearch=mysql_query("select summary from slips where sid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $slipSummary=$row['summary'];
  }
  dbdesconectar();
  
  if ( $slipSummary == '' )
  {
    $slipSummary='Ninguno';
  }
  
  return $slipSummary;
}

function slipStatus()
{
  $output=array();
  dbconectar();
  $query="select * from status";
  $searchStatus=mysql_query($query);
  while($row=mysql_fetch_array($searchStatus))
  {
    $statusId=$row['flag'];
    $statusDescription=$row['label'];
    $output[]=array("$statusId", "$statusDescription");
  }
  
  dbdesconectar();
  return ($output);
}

function slipStatusFromSlipId($slipId) {
  dbconectar();
  $slipSearch=mysql_query("select b.label as status from slips a, status b where a.status=b.flag and sid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $slipStatus=$row['status'];
  }
  dbdesconectar();
  return $slipStatus;
}

function slipParentFromSlipId($slipId) {
  dbconectar();
  $slipSearch=mysql_query("select spid from slips where sid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $value=$row['spid'];
  }
  if (( $value == '' ) OR ( $value == '0'))
  {
    $parentSlip='Ninguno';
  } else {
    $parentSlipId=$value;
    $slipSearch=mysql_query("select problem from slips where sid='$parentSlipId'");
    while ($row=mysql_fetch_array($slipSearch)) {
      $parentSlipDetail=$row['problem'];
    }
    $parentSlip="<a href=\"desktop.php?function=slip_details&slipId=$parentSlipId\">$parentSlipId</a> - $parentSlipDetail";
  }
  dbdesconectar();
  return $parentSlip;
}

function slipChildFromSlipId($slipId) {
  dbconectar();
  $childCount=0;
  $slipSearch=mysql_query("select sid,problem from slips where spid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $slipChildId=$row['sid'];
    $slipChildProblem=$row['problem'];
    if ( $slipChildId <> '' ) {
      $childCount=$childCount + 1;
    }
  }
  dbdesconectar();
  if ( $childCount == 0 )
  {
    $childCount='Ninguno';
  }
  return $childCount;
}

function slipCompanyFromSlipId($slipId) {
  dbconectar();
  $slipSearch=mysql_query("select b.razon_social as company from slips a, empresas b where a.empresa_id=b._rowid and sid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $slipCompany=$row['company'];
  }
  dbdesconectar();
  
  if ( $slipCompany == '' )
  {
    $slipCompany='N/D';
  }
  
  return $slipCompany;
}

function slipOpenDateFromSlipId($slipId) {
  dbconectar();
  $slipSearch=mysql_query("select o_date from slips where sid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $slipOpenDateTmp=$row['o_date'];
    $slipOpenDate=date("d/m/Y h:i",$slipOpenDateTmp);
  }
  dbdesconectar();
  return $slipOpenDate;
}

function slipOpenTechFromSlipId($slipId) 
{
  dbconectar();
  $slipOpenTechSearch=mysql_query("select a.login as login from people a, slips b where b.o_peo_id=a.peo_id and b.sid='$slipId'");
  while($row=mysql_fetch_array($slipOpenTechSearch))
  {
    $slipOpenTech=$row['login'];
  }
  
  dbdesconectar();
  
  if (( $slipOpenTech == '') OR ( $slipOpenTech == ''))
  {
    $slipOpenTech='No Asignado';
  }
  
  return $slipOpenTech;
}

function slipCurrTechFromSlipId($slipId) 
{
  dbconectar();
  $slipCurrTechSearch=mysql_query("select a.login as login from people a, slips b where b.c_peo_id=a.peo_id and b.sid='$slipId'");
  while($row=mysql_fetch_array($slipCurrTechSearch))
  {
    $slipCurrTech=$row['login'];
  }
  
  dbdesconectar();
  
  if (( $slipCurrTech == '') OR ( $slipCurrTech == ''))
  {
    $slipCurrTech='No Asignado';
  }
  
  return $slipCurrTech;
}

function slipLastTechFromSlipId($slipId) 
{
  dbconectar();
  $slipLastTechSearch=mysql_query("select a.login as login from people a, slips b where b.l_peo_id=a.peo_id and b.sid='$slipId'");
  while($row=mysql_fetch_array($slipLastTechSearch))
  {
    $slipLastTech=$row['login'];
  }
  
  dbdesconectar();
  
  if (( $slipLastTech == '') OR ( $slipLastTech == ''))
  {
    $slipLastTech='N/D';
  }
  
  return $slipLastTech;
}

function slipLastActivityFromSlipId($slipId)
{
  dbconectar();
  $slipLastActivitySearch=mysql_query("select l_activity from slips where sid='$slipId'");
  while($row=mysql_fetch_array($slipLastActivitySearch))
  {
    $slipLastActivityTemp=$row['l_activity'];
    $slipLastActivity=date("d/m/Y h:i",$slipLastActivityTemp);
  }
  
  dbdesconectar();
  
  if (( $slipLastActivity == '') OR ( $slipLastActivity == ''))
  {
    $slipLastActivity='N/D';
  }
  
  return $slipLastActivity;
}

function slipProblemFromSlipId($slipId) {
  dbconectar();
  $slipSearch=mysql_query("select problem from slips where sid='$slipId'");
  while ($row=mysql_fetch_array($slipSearch)) {
    $slipProblem=$row['problem'];
  }
  dbdesconectar();
  return $slipProblem;
}

function slipPriorityFromSlipId($slipId) {
  dbconectar();
  $slipPrioritySearch=mysql_query("select priority from slips where sid='$slipId'");
  while ($row=mysql_fetch_array($slipPrioritySearch)) {
    $slipPriority=$row['priority'];
  }
  dbdesconectar();
  if ( $slipPriority == '' )
  {
    $slipPriority='N/A';
  }
  
  return $slipPriority;
}

function slipFollowupsFromSlipId($slipId) {
  dbconectar();
  $followupSearch=mysql_query("select * from followups where f_sid='$slipId' order by f_seq desc");
  while ($row=mysql_fetch_array($followupSearch)) {
    $followupSeq=$row['f_seq'];
    $followupAuthor=$row['f_tech'];
    $followupDateTmp=$row['f_date'];
    $followupDate=date("d/m/Y h:i",$followupDateTmp);
    $followupDetailTmp=$row['f_text'];
    /* $tmpText=htmlspecialchars(stripslashes($followupDetailTmp)); */
    /* $tmpText=ereg_replace("\n","<br>","$tmpText"); */
    $tmpText=ereg_replace("\n","<br>",$followupDetailTmp);
    $followupDetail=$tmpText;
    $followupTime=$row['f_time'];
    $followupValue='P';
    $followupMod='M';
    echo "                <tr>";
    echo "                  <td class=\"rtdh2\"><div align=\"left\">";
    echo "$followupSeq";
    echo "                  </div></td>";
    echo "                  <td class=\"rtdh2\"><div align=\"left\">";
    echo "$followupAuthor";
    echo "                  </div></td>";
    echo "                  <td class=\"rtdh2\"><div align=\"left\">";
    echo "$followupDate";
    echo "                  </div></td>";
    echo "                  <td class=\"rtdh2\"><div align=\"left\">";
    echo "$followupDetail";
    echo "                  </div></td>";
    echo "                  <td class=\"rtdh2\"><div align=\"left\">";
    echo "$followupTime";
    echo "                  </div></td>";
    echo "                  <td class=\"rtdh2\"><div align=\"left\">";
    echo "$followupValue";
    echo "                  </div></td>";
    echo "                  <td class=\"tdh2\"><div align=\"left\">";
    echo "$followupMod";
    echo "                  </div></td>";
    echo "                </tr>";
  }
  dbdesconectar();
}

function uniqueCompany($companyName,$companyAddressStreet,$companyAddressNumber,$companyAddressTown,$companyAddressState,$companyAddressCountry,$companyNumber,$companyCondition)
{
  include '../conf/config.php';
  dbconectar();
  $searchCompany=mysql_query("select count(*) as quantity from $customersTable where razon_social='$companyName' and addressstreet='$companyAddressStreet' and addressnumber='$companyAddressNumber' and addresstown='$companyAddressTown' and addressstate='$companyAddressState' and addresscountry='$companyAddressCountry' and number='$companyNumber' and taxcondition='$companyCondition'");
  // echo "select count(*) as quantity from $customersTable where razon_social='$companyName' and addressstreet='$companyAddressStreet' and addressnumber='$companyAddressNumber' and addresstown='$companyAddressTown' and addressstate='$companyAddressState' and addresscountry='$companyAddressCountry' and number='$companyNumber' and taxcondition='$companyCondition'";
  while ($row=mysql_fetch_array($searchCompany))
  {
    $quantity=$row['quantity'];
    // echo "Cantidad ".$quantity;
    if ($quantity == 0)
    {
      $output=true;
    } else {
      $output=false;
    }
  }
  dbdesconectar();
  
  return($output);
}

?>
